home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / pvm34b3.zip / pvm34b3 / pvm3 / examples / bwtest.c next >
C/C++ Source or Header  |  1997-07-22  |  7KB  |  310 lines

  1.  
  2. static char rcsid[] =
  3.     "$Id: bwtest.c,v 1.7 1997/07/09 13:24:40 pvmsrc Exp $";
  4.  
  5. /*
  6.  *         PVM version 3.4:  Parallel Virtual Machine System
  7.  *               University of Tennessee, Knoxville TN.
  8.  *           Oak Ridge National Laboratory, Oak Ridge TN.
  9.  *                   Emory University, Atlanta GA.
  10.  *      Authors:  J. J. Dongarra, G. E. Fagg, M. Fischer
  11.  *          G. A. Geist, J. A. Kohl, R. J. Manchek, P. Mucci,
  12.  *         P. M. Papadopoulos, S. L. Scott, and V. S. Sunderam
  13.  *                   (C) 1997 All Rights Reserved
  14.  *
  15.  *                              NOTICE
  16.  *
  17.  * Permission to use, copy, modify, and distribute this software and
  18.  * its documentation for any purpose and without fee is hereby granted
  19.  * provided that the above copyright notice appear in all copies and
  20.  * that both the copyright notice and this permission notice appear in
  21.  * supporting documentation.
  22.  *
  23.  * Neither the Institutions (Emory University, Oak Ridge National
  24.  * Laboratory, and University of Tennessee) nor the Authors make any
  25.  * representations about the suitability of this software for any
  26.  * purpose.  This software is provided ``as is'' without express or
  27.  * implied warranty.
  28.  *
  29.  * PVM version 3 was funded in part by the U.S. Department of Energy,
  30.  * the National Science Foundation and the State of Tennessee.
  31.  */
  32.  
  33. /* bwtest.c - bandwidth tester
  34.  * This is yet another bandwidth tester.
  35.  * It measures user-space to user-space times.
  36.  *
  37.  * The measurement is a standard ping-pong with roundtrip time measured
  38.  * from the originating sender.
  39.  *
  40.  * This is a PVM test code
  41.  */
  42.  
  43. #include "pvm3.h"
  44. #include "stdio.h"        /* needed to help 64bit compilers understand printf */
  45. #ifndef SAMPLE
  46. #define SAMPLE  20     /* sample rate */
  47. #endif
  48. #ifndef MAXSIZE
  49. #define MAXSIZE    100000    /* must be a power of 10 */
  50. #endif
  51.  
  52. #define VTAG 10
  53. #define MTAG 100
  54.  
  55. #ifndef ENCODE
  56. #define ENCODE    PvmDataInPlace
  57. #endif
  58.  
  59. #define GETTIME(_x,_y) pvmgetclock((_x)) 
  60.  
  61. #ifndef WIN32
  62. #include <sys/time.h>
  63. #else
  64. #include <winsock.h>
  65. #define sleep(a) Sleep(a)
  66. #endif
  67.  
  68. struct timeval tv1, tv2;
  69. #define TIMER_CLEAR     (tv1.tv_sec = tv1.tv_usec = tv2.tv_sec = tv2.tv_usec =0)
  70. #define TIMER_START     GETTIME(&tv1, (struct timezone*)0)
  71. #define TIMER_STOP      GETTIME(&tv2, (struct timezone*)0)
  72. #define TIMER_ELAPSED    (tv2.tv_sec-tv1.tv_sec+(tv2.tv_usec-tv1.tv_usec)*1.E-6)
  73.  
  74.  
  75. #define GNAME "bwtest"
  76.  
  77. void pingpong();    /* where all the work is done and measured */
  78.  
  79.  
  80. main(argc, argv)
  81. int argc;
  82. char *argv[];
  83. {
  84.     int mytid;
  85.     int myinst;
  86.     int othertid;
  87.     int otherinst;
  88.  
  89.     if ((mytid = pvm_mytid()) < 0)
  90.         exit(-1);
  91.  
  92.     if((myinst = pvm_joingroup(GNAME)) < 0) {
  93.         pvm_perror("Error joining bwtest group");
  94.         exit(-1);
  95.     }
  96.  
  97.  
  98.     printf(" %x myinst is %d \n", mytid, myinst);
  99.     switch (myinst % 2) {
  100.     case 0: 
  101.  
  102.         otherinst = myinst + 1;
  103.         while ((othertid = pvm_gettid(GNAME, otherinst)) < 0)
  104.             sleep(1);
  105.  
  106.         pingpong(mytid, othertid);
  107.         break;
  108.     case 1:
  109.         otherinst = myinst - 1;
  110.         while ((othertid = pvm_gettid(GNAME, otherinst)) < 0)
  111.             sleep(1);
  112.  
  113.         pingpong(othertid, mytid);
  114.         break;
  115.     }
  116.  
  117.     pvm_lvgroup(GNAME);
  118.     pvm_exit();
  119.  
  120. }
  121. void
  122. validate(data, size)
  123.     double data[];
  124.     int size;
  125. {
  126.     int i;
  127.     int mytid;
  128.  
  129.     mytid = pvm_mytid();
  130.  
  131.     for (i = 0; i < size; i++)
  132.         if ((i*i - data[i]) > 0.01) {
  133.             printf("error: data[%d] = %g\n", i, data[i]);
  134.             break;
  135.         }
  136.  
  137.     if (i == size)
  138.         printf("t%x: %d doubles received correctly\n\n\n", mytid, i);
  139. }
  140.  
  141.  
  142. /* exchange messages and measure the transit time */
  143. void
  144. pingpong(mastertid, slavetid)
  145. int mastertid;
  146. int slavetid;
  147. {
  148.     int ismaster;
  149.     int mytid;
  150.     int n;
  151.     int size;
  152.     int t;
  153.  
  154.     static double data[MAXSIZE];
  155.  
  156.     char str[32];
  157.  
  158.     /* test node-to-node send */
  159.  
  160.     mytid = pvm_mytid();
  161.  
  162.  
  163.     ismaster = ( mytid ==  mastertid );
  164.  
  165.  
  166.     if (ismaster)
  167.     {
  168. #ifdef PACK
  169.         print_header(ENCODE, MAXSIZE*sizeof(double), SAMPLE);
  170. #else
  171.         print_header(-1, MAXSIZE*sizeof(double), SAMPLE);
  172. #endif
  173.     }
  174.  
  175.     printf("%x -- I am the %s \n", mytid, (ismaster ? "master" : "slave"));
  176.  
  177.  
  178.     /* first validate that all doubles are sent and returned correctly */
  179.     if (ismaster)
  180.     {
  181.  
  182.         for (n = 0; n < MAXSIZE; n++)
  183.             data[n] = n*n;
  184. #ifdef PACK
  185.         pvm_initsend(ENCODE);
  186.         pvm_pkdouble(data, MAXSIZE, 1);
  187.         pvm_send(slavetid, VTAG); /* send the data */
  188.         
  189.         pvm_recv(slavetid, VTAG); /* recv it back and validate */
  190.         pvm_upkdouble(data, MAXSIZE, 1);
  191. #else
  192.         pvm_psend(slavetid, VTAG, data, MAXSIZE, PVM_DOUBLE);
  193.         pvm_precv(slavetid, VTAG, data, MAXSIZE, PVM_DOUBLE, (int*)0, 
  194.             (int*)0, (int*)0);
  195. #endif
  196.         
  197.     }
  198.     else
  199.     {
  200. #ifdef PACK
  201.         
  202.         pvm_recv(mastertid, VTAG);    /* recv and unpack */ 
  203.         pvm_upkdouble(data, MAXSIZE, 1);
  204.  
  205.         pvm_initsend(ENCODE);        /* echo */
  206.         pvm_pkdouble(data, MAXSIZE, 1);
  207.         pvm_send(mastertid, VTAG); /* send the data */
  208. #else
  209.         pvm_precv(mastertid, VTAG, data, MAXSIZE, PVM_DOUBLE, (int*)0, 
  210.             (int*)0, (int*)0);
  211.         pvm_psend(mastertid, VTAG, data, MAXSIZE, PVM_DOUBLE);
  212. #endif
  213.     }
  214.  
  215.     validate(data, MAXSIZE);
  216.     sprintf(str, "%d doubles from t%x", MAXSIZE, slavetid);
  217.     
  218.  
  219.     /* now do the ping-pong */
  220.  
  221.     for (size = 0; size <= MAXSIZE; size = ( size ? 10*size : 1) )
  222.     {
  223.         TIMER_CLEAR;
  224.         TIMER_START;
  225.         for (n = 0; n < SAMPLE; n++) 
  226.         {
  227.             if (ismaster)
  228.             {
  229. #ifdef PACK
  230.                 pvm_initsend(ENCODE);
  231.                 pvm_pkdouble(data, size, 1);
  232.                 pvm_send(slavetid, MTAG);
  233.                 pvm_recv(slavetid, MTAG);
  234.                 pvm_upkdouble(data, size, 1);
  235. #else
  236.                 pvm_psend(slavetid, MTAG, data, size, PVM_DOUBLE);
  237.                 pvm_precv(slavetid, MTAG, data, size, PVM_DOUBLE, 
  238.                         (int*)0, (int*)0, (int*)0);
  239. #endif /*PACK*/
  240.             }
  241.             else
  242.             {
  243. #ifdef PACK
  244.                 pvm_recv(mastertid, MTAG);
  245.                 pvm_upkdouble(data, size, 1);
  246.                 pvm_initsend(ENCODE);
  247.                 pvm_pkdouble(data, size, 1);
  248.                 pvm_send(mastertid, MTAG);
  249. #else
  250.                 pvm_precv(mastertid, MTAG, data, size, PVM_DOUBLE, 
  251.                         (int*)0, (int*)0, (int*)0);
  252.                 pvm_psend(mastertid, MTAG, data, size, PVM_DOUBLE);
  253. #endif /*PACK*/
  254.             }
  255.         }
  256.         if (ismaster)
  257.         {
  258.             TIMER_STOP;
  259.             t = 1000000*TIMER_ELAPSED/SAMPLE;
  260.             printf("Roundtrip T = %d (us)  (%.4f MB/s)  Data size: %d\n",
  261.                 t, 2.0*8.0*(float)size/(float)t, sizeof(double)*size);
  262.         }
  263.     }
  264. }
  265.  
  266.  
  267. int
  268. print_header(packtype, size, niter)
  269. int packtype;
  270. int size;
  271. int niter;
  272. {
  273.     printf("--- Simple PVM Bandwidth Test ----\n");
  274.     printf(" Using pack option: %s \n",
  275.         (packtype == PvmDataRaw ? "PvmDataRaw" : 
  276.         (packtype == PvmDataInPlace ? "PvmDataInPlace" :
  277.         (packtype == PvmDataDefault ? "PvmDataDefault" :
  278.         (packtype == -1 ? "Psend/Precv" : "Unknown")))));
  279.     printf(" Max data size is: %d \n", size);
  280.     printf(" Number of iterations/sample: %d \n", niter);
  281.     printf(" \n\n");
  282.     printf(" Roundtrip time is measured from user-space to user-space.\n");
  283.     if ( packtype != -1)
  284.     {
  285.         printf(" For packed messages this includes the combined time of: \n"); 
  286.         printf("    inst 0: pvm_initsend() \n"); 
  287.         printf("    inst 0: pvm_pack() \n"); 
  288.         printf("    inst 0: pvm_send() \n"); 
  289.         printf("    inst 1: pvm_recv() \n");
  290.         printf("    inst 1: pvm_unpack() \n");
  291.         printf("    inst 1: pvm_initsend() \n");
  292.         printf("    inst 1: pvm_pack() \n");
  293.         printf("    inst 1: pvm_send() \n");
  294.         printf("    inst 0: pvm_recv() \n");
  295.         printf("    inst 0: pvm_unpack() \n");
  296.     }
  297.     else
  298.     {
  299.         printf(" For pvm_psend/precv this includes the combined time of: \n"); 
  300.         printf("    inst 0: pvm_psend() \n"); 
  301.         printf("    inst 1: pvm_precv() \n"); 
  302.         printf("    inst 1: pvm_psend() \n"); 
  303.         printf("    inst 0: pvm_precv() \n");
  304.     }
  305.     
  306.     printf("\n\n---------------------------------------\n");
  307.     
  308.     return 0;
  309. }
  310.